﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using PaydSystem.Business;
using PaydSystem.Common;
using PaydSystem.Cls;
using System.Web.Script.Serialization;
using System.Globalization;

namespace PaydSystem
{
    /// <summary>
    /// Summary description for PaydSw
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class PaydSw : System.Web.Services.WebService
    {
        #region Payd User
        [WebMethod(EnableSession = true)]
        public Object GetListUser()
        {
            Fn Utility = new Fn();
            PaydUserBiz conDataUserBiz = new PaydUserBiz();
            List<PaydUserCls> listData = new List<PaydUserCls>();

            try
            {

                listData = conDataUserBiz.GetListDataBiz();

                return new JavaScriptSerializer().Serialize(new
                {
                    Result = 1,
                    objData = listData,

                });


            }
            catch (Exception e)
            {
                FunctionCommon Confn = new FunctionCommon();
                Confn.Logger("Error at GetListUser in PaydSw " + e.Message.ToString() + " " + e.StackTrace.ToString());
                Utility.ClearSession();
                throw;
            }
        }
        [WebMethod(EnableSession = true)]
        public Object PaydCheckUser(string idCard, string language)
        {
            Fn Utility = new Fn();
            PaydUserBiz conDataBiz = new PaydUserBiz();
            var result = 0;
            var messageContent = "";
            var messageTitle = "";
            try
            {
                result = conDataBiz.PaydCheckUserBiz(idCard);
                if (result == 0)
                {
                    if (language == "TH")
                    {
                        messageTitle = "ข้อความเตือน";
                        messageContent = "หมายเลขประชาชนไม่มีในระบบ,กรุณาเช็ค";
                    }
                    else if (language == "EN")
                    {
                        messageTitle = "Warning Message";
                        messageContent = "Id card number is not in System.,Please check.";
                    }
                }
                else if (result == 2)
                {

                    if (language == "TH")
                    {
                        messageTitle = "ข้อความเตือน";
                        messageContent = "หมายเลขประชาชนนี้ได้ถูกใช้งานไปแล้ว,กรุณาล็อกอินเข้าสู่ระบบ";
                    }
                    else if (language == "EN")
                    {
                        messageTitle = "Warning Message";
                        messageContent = "Id card number was in use already.,Please log in.";
                    }
                }
                else
                {
                    messageTitle = "";
                    messageContent = "";
                }
                return new JavaScriptSerializer().Serialize(new
                {
                    Result = result,
                    MessageTitle = messageTitle,
                    MessageContent = messageContent
                });

            }
            catch (Exception e)
            {
                FunctionCommon Confn = new FunctionCommon();
                Confn.Logger("Error at PaydCheckUser in PaydSw " + e.Message.ToString() + " " + e.StackTrace.ToString());
                Utility.ClearSession();
                throw;
            }
        }
        [WebMethod(EnableSession = true)]
        public Object GetUserByIdCard(string idCard)
        {
            Fn Utility = new Fn();
            PaydUserBiz conDataUserBiz = new PaydUserBiz();
            PaydUserCls recDataUser = new PaydUserCls();

            try
            {

                recDataUser = conDataUserBiz.GetRecDataByIdCardBiz(idCard);

                return new JavaScriptSerializer().Serialize(new
                {
                    Result = 1,
                    objData = recDataUser,

                });


            }
            catch (Exception e)
            {
                FunctionCommon Confn = new FunctionCommon();
                Confn.Logger("Error at GetUserIdByIdCard in PaydSw " + e.Message.ToString() + " " + e.StackTrace.ToString());
                Utility.ClearSession();
                throw;
            }
        }
        [WebMethod(EnableSession = true)]
        public Object GetUserByMemberId(string memberId)
        {
            Fn Utility = new Fn();
            PaydUserBiz conDataUserBiz = new PaydUserBiz();
            PaydUserCls recDataUser = new PaydUserCls();

            try
            {

                recDataUser = conDataUserBiz.GetRecDataByMemberIdBiz(memberId);

                return new JavaScriptSerializer().Serialize(new
                {
                    Result = 1,
                    objData = recDataUser,

                });


            }
            catch (Exception e)
            {
                FunctionCommon Confn = new FunctionCommon();
                Confn.Logger("Error at GetUserByMemberId in PaydSw " + e.Message.ToString() + " " + e.StackTrace.ToString());
                Utility.ClearSession();
                throw;
            }
        }
        [WebMethod(EnableSession = true)]
        public Object SavePaydUser(PaydUserCls paydUser, string language)
        {
            Fn Utility = new Fn();
            PaydUserBiz conDataBiz = new PaydUserBiz();
            FunctionCommon fnCommon = new FunctionCommon();
            var ActivatePath = DefaultValue.strActivatePath;
            var result = 0;
            var messageContent = "";
            var messageTitle = "";
            try
            {

                result = conDataBiz.SavePaydUserBiz(paydUser);
                if (result == 2)
                {
                    if (language == "TH")
                    {
                        messageTitle = "ข้อความเตือน";
                        messageContent = "อีเมล์หลักนี่ได้ถูกใช้งานไปแล้ว,โปรดเปลี่ยนอีเมล์หลัก";
                    }
                    else if (language == "EN")
                    {
                        messageTitle = "Warning Message";
                        messageContent = "This primary e-mail is in use,Please change primary e-mail";
                    }
                }
                else if (result == 3)
                {
                    if (language == "TH")
                    {
                        messageTitle = "ข้อความเตือน";
                        messageContent = "อีเมล์สำรองนี่ได้ถูกใช้งานไปแล้ว,โปรดเปลี่ยนอีเมล์สำรอง";
                    }
                    else if (language == "EN")
                    {
                        messageTitle = "Warning Message";
                        messageContent = "This secondary e-mail is in use,Please change secondary e-mail";
                    }
                }
                else if (result == 1)
                {
                    DateTime today = DateTime.Today;

                    if (language == "TH")
                    {
                        string dt = today.ToString("dd/MM/yyyy", new CultureInfo("th-TH"));
                        String[] info = new String[] {
                            paydUser.Title + ' ' + paydUser.Name
                            ,dt
                            ,ActivatePath + paydUser.MemberId
                        };
                        fnCommon.SendEmail(paydUser.EmailPrimary,"ยินดีที่ต้อนรับสู่ระบบ Payd", info ,DefaultValue.strTemplateEmail);
                        messageTitle = "ผลลัพธ์การบันทึก";
                        messageContent = "บันทึกข้อมูลลูกค้าเรียบร้อยแล้ว โปรดเช็คอีเมล์หลักเพื่อ Activate บัญชีการใช้งาน";
                    }
                    else if (language == "EN")
                    {
                        string dt = today.ToString("dd/MM/yyyy", new CultureInfo("en-US"));
                        String[] info = new String[] {
                            paydUser.Title + ' ' + paydUser.Name
                            ,dt
                            ,ActivatePath + paydUser.MemberId
                        };
                        fnCommon.SendEmail(paydUser.EmailPrimary, "Welcome to Payd System.,Please check email for activate account.", info, DefaultValue.strTemplateEmail);
                        messageTitle = "Save Result";
                        messageContent = "Save profile successfully.";
                    }
                }
                else
                {
                    if (language == "TH")
                    {
                        messageTitle = "ข้อความเตือน";
                        messageContent = "ไม่สามารถบันทึกได้,โปรดเช็ค";
                    }
                    else if (language == "EN")
                    {
                        messageTitle = "Warning Message";
                        messageContent = "Can not save profile.,Please check";
                    }
                }

                return new JavaScriptSerializer().Serialize(new
                {
                    Result = result,
                    MessageTitle = messageTitle,
                    MessageContent = messageContent
                });
            }
            catch (Exception e)
            {
                FunctionCommon Confn = new FunctionCommon();
                Confn.Logger("Error at SavePaydUser in PaydSw " + e.Message.ToString() + " " + e.StackTrace.ToString());
                Utility.ClearSession();
                throw;
            }
        }
        [WebMethod(EnableSession = true)]
        public Object CheckUserExistLogin()
        {
            Fn Utility = new Fn();
            var result = 0;
            PaydUserBiz conPaydUserBiz = new PaydUserBiz();
            PaydUserCls recPaydUser = new PaydUserCls();

            try
            {

                if (HttpContext.Current.Session["UserAuthentication"] != null)
                {
                    recPaydUser = conPaydUserBiz.GetRecDataByMemberIdBiz(HttpContext.Current.Session["UserAuthentication"].ToString().Trim());
                    result = 1;
                }
                else
                {
                    result = 0;
                }
                return new JavaScriptSerializer().Serialize(new
                {
                    Result = result,
                    RecData = recPaydUser
                });

            }
            catch (Exception e)
            {
                FunctionCommon Confn = new FunctionCommon();
                Confn.Logger("Error at CheckUserExistLogin in PaydSw " + e.Message.ToString() + " " + e.StackTrace.ToString());
                Utility.ClearSession();
                throw;
            }

        }
        [WebMethod(EnableSession = true)]
        public int ActivateUser(string memberId)
        {
            
            Fn Utility = new Fn();
            PaydUserBiz conPaydUserBiz = new PaydUserBiz();
            

            try
            {

                var result = conPaydUserBiz.ActivateUserBiz(memberId);
                return result;

            }
            catch (Exception e)
            {
                FunctionCommon Confn = new FunctionCommon();
                Confn.Logger("Error at ActivateUser in PaydSw " + e.Message.ToString() + " " + e.StackTrace.ToString());
                Utility.ClearSession();
                throw;
            }
        }
        [WebMethod(EnableSession = true)]
        public Object CheckUserLogin(string username, string password)
        {
            Fn Utility = new Fn();
            PaydUserBiz conPaydUserBiz = new PaydUserBiz();
            PaydUserCls recPaydUser = new PaydUserCls();

            try
            {

                var result = conPaydUserBiz.CheckUserLoginBiz(username, password);
                if (result == 1)
                {
                    recPaydUser = conPaydUserBiz.GetRecDataByEmailPrimaryBiz(username);
                    Utility.CreatePaydUserSession(recPaydUser, password);
                }
                else if (result == 2)
                {
                    recPaydUser = conPaydUserBiz.GetRecDataByUserIdBiz(username);
                    Utility.CreatePaydUserSession(recPaydUser, password);
                }
                else
                {
                    Utility.ClearSession();
                }
                return new JavaScriptSerializer().Serialize(new
                {
                    Result = result,
                    RecData = recPaydUser
                });

            }
            catch (Exception e)
            {
                FunctionCommon Confn = new FunctionCommon();
                Confn.Logger("Error at CheckCustomerLogin in PaydSw " + e.Message.ToString() + " " + e.StackTrace.ToString());
                Utility.ClearSession();
                throw;
            }
        }
        [WebMethod(EnableSession = true)]
        public int SendForgetPwd(string email,string language)
        {
            Fn Utility = new Fn();
            PaydUserBiz conPaydUserBiz = new PaydUserBiz();
            PaydUserCls paydUser = new PaydUserCls();
            FunctionCommon fnCommon = new FunctionCommon();
            var ForgetEmailPath = DefaultValue.strForgetEmailPath;
            try
            {
                var result = conPaydUserBiz.SendForgetPwdBiz(email);
                if (result == 1)
                {
                    DateTime today = DateTime.Today;
                    paydUser = conPaydUserBiz.GetRecDataByEmailPrimaryBiz(email);
                    if (language == "TH")
                    {
                        string dt = today.ToString("dd/MM/yyyy", new CultureInfo("th-TH"));
                        String[] info = new String[] {
                            paydUser.Title + ' ' + paydUser.Name
                            ,dt
                            ,ForgetEmailPath + paydUser.MemberId
                        };
                        fnCommon.SendEmail(email, "แจ้งแก้ไข Password ระบบ Payd", info, DefaultValue.strTemplateEmailForgetEmail);
                    }
                    else if (language == "EN")
                    {
                        string dt = today.ToString("dd/MM/yyyy", new CultureInfo("en-US"));
                        String[] info = new String[] {
                            paydUser.Title + ' ' + paydUser.Name
                            ,dt
                            ,ForgetEmailPath + paydUser.MemberId
                        };
                        fnCommon.SendEmail(paydUser.EmailPrimary, "Warning edit password for access Payd System.", info, DefaultValue.strTemplateEmailForgetEmail);
                        
                    }
                }
                return result;

            }
            catch (Exception e)
            {
                FunctionCommon Confn = new FunctionCommon();
                Confn.Logger("Error at SendForgetPwd in PaydSw " + e.Message.ToString() + " " + e.StackTrace.ToString());
                Utility.ClearSession();
                throw;
            }
        }
        [WebMethod(EnableSession = true)]
        public int SavePwdForForget(string memberId, string password)
        {
            Fn Utility = new Fn();
            PaydUserBiz conPaydUserBiz = new PaydUserBiz();
            
            try
            {

                var result = conPaydUserBiz.SavePwdForForgetBiz(memberId, password);
                return result;
                

            }
            catch (Exception e)
            {
                FunctionCommon Confn = new FunctionCommon();
                Confn.Logger("Error at SavePwdForForget in PaydSw " + e.Message.ToString() + " " + e.StackTrace.ToString());
                Utility.ClearSession();
                throw;
            }
        }
        [WebMethod(EnableSession = true)]
        public int ChkExpireChangePwd(string memberId)
        {
            Fn Utility = new Fn();
            PaydUserBiz conPaydUserBiz = new PaydUserBiz();

            try
            {

                var result = conPaydUserBiz.ChkExpireChangePwdBiz(memberId);
                return result;


            }
            catch (Exception e)
            {
                FunctionCommon Confn = new FunctionCommon();
                Confn.Logger("Error at ChkExpireChangePwd in PaydSw " + e.Message.ToString() + " " + e.StackTrace.ToString());
                Utility.ClearSession();
                throw;
            }
        }
        [WebMethod(EnableSession = true)]
        public Object GetProfileUser()
        {
            Fn Utility = new Fn();
            PaydUserBiz conDataUserBiz = new PaydUserBiz();
            PaydUserCls recDataUser = new PaydUserCls();
            try
            {
                if (HttpContext.Current.Session["UserAuthentication"] != null)
                {
                    
                    recDataUser = conDataUserBiz.GetRecDataByMemberIdBiz(HttpContext.Current.Session["UserAuthentication"].ToString().Trim());
                    if ((recDataUser.RegisterFlag == "A") || (recDataUser.Status == "A"))
                    {
                        return new JavaScriptSerializer().Serialize(new
                        {
                            Result = 1,
                            objUserData = recDataUser
                        });
                    }
                    else
                    {
                        Utility.ClearSession();
                        return new JavaScriptSerializer().Serialize(new
                        {
                            Result = 99,
                            objUserData = recDataUser
                        });
                    }
                }
                else
                {
                    Utility.ClearSession();
                    return new JavaScriptSerializer().Serialize(new
                    {
                        Result = 99,
                        objUserData = recDataUser
                    });
                }
            }
            catch (Exception e)
            {
                FunctionCommon Confn = new FunctionCommon();
                Confn.Logger("Error at GetProfileByIdCard in PaydSw " + e.Message.ToString() + " " + e.StackTrace.ToString());
                Utility.ClearSession();
                throw;
            }
        }
        [WebMethod(EnableSession = true)]
        public int LogOutAction()
        {
            Fn Utility = new Fn();
            //User.userName = DefaultValue.checkauthenUserName;
            //User.password = DefaultValue.checkauthenPassword;
            try
            {
                //if (User != null)
                //{
                //    if (User.Isvalid())
                //    {
                if (HttpContext.Current.Session["UserAuthentication"] != null)
                {
                    Utility.ClearSession();
                    return 1;
                }
                else
                {
                    Utility.ClearSession();
                    return 99;
                }
                //}
                //else
                //{
                //    Utility.ClearSession();
                //    return 99;
                //}
                //    }
                //    else
                //    {
                //        Utility.ClearSession();
                //        return 99;
                //    }
            }
            catch (Exception e)
            {
                FunctionCommon Confn = new FunctionCommon();
                Confn.Logger("Error at LogOutAction in PaydSw " + e.Message.ToString() + " " + e.StackTrace.ToString());
                Utility.ClearSession();
                throw;
            }

        }

        #endregion
    }
}
